from menu_button import MenuButton
from panda3d.core import Vec2, Point3, Vec3, Vec4
import os
from direct.gui.DirectGui import *
from pandac.PandaModules import TextNode
from dialog import Dialog
'''
	Andrew Oberlin
	Date: March 9, 2012
'''
class MapSelector:
	def __init__(self, parent, singlePlayer):
		self.parent = parent
		self.singlePlayer = singlePlayer
		self.background = self.parent.loader.loadModelCopy("models/plane")
		self.backgroundTexture = self.parent.loader.loadTexture("models/menu/hud/level_selector.png")
		self.background.setScale(2.67, 1, 2)
		self.background.setTexture(self.backgroundTexture)
		self.background.setTransparency(1)
		self.background.reparentTo(self.parent.base.aspect2d)
	
		self.dialog = None
		self.parent.accept("h", self.showInformationDialog)
	
		# find all known level files
		self.buttons = {}
		
		self.buttons['level_one'] = MenuButton(self.parent, "level_one", "models/menu/hud/file_icon", self.setSelection, Vec3(-0.75,0,0.40), 
			isMovie=False, scale=Vec3(0.60, 2, 0.10), text="Level One", textPos=Vec3(-0.2,0,-0.020), textScale=Vec3(0.05,0.05,0.0375), 
			extraArgs=['level_one'])
			
		self.buttons['level_two'] = MenuButton(self.parent, "level_two", "models/menu/hud/file_icon", self.setSelection, Vec3(-0.75,0,0.30), 
			isMovie=False, scale=Vec3(0.60, 2, 0.10), text="Level Two", textPos=Vec3(-0.2,0,-0.020), textScale=Vec3(0.05,0.05,0.0375), 
			extraArgs=['level_two'])
			
		self.buttons['level_three'] = MenuButton(self.parent, "level_three", "models/menu/hud/file_icon", self.setSelection, Vec3(-0.75,0,0.20), 
			isMovie=False, scale=Vec3(0.60, 2, 0.10), text="Level Three", textPos=Vec3(-0.2,0,-0.020), textScale=Vec3(0.05,0.05,0.0375), 
			extraArgs=['level_three'])
			
		self.input = DirectEntry(initialText="Enter a filename", cursorKeys=True, scale=0.05, 
			focusInCommand=self.focusInput)
		self.input.setPos(Vec3(-1.05,0,0.05))
		self.input.reparentTo(self.parent.base.aspect2d)
		
		self.buttons['load_custom'] = MenuButton(self.parent, "level_three", "models/menu/hud/map_selector_button", self.setCustomSelection, Vec3(-0.30,0,0.065), 
			isMovie=False, scale=Vec3(0.35, 2, 0.10), text="Load Custom", textPos=Vec3(-0.15,0,-0.0125), textScale=Vec3(0.05,0.05,0.0375))
		
		self.buttons['ok'] = MenuButton(self.parent, "ok", "models/menu/hud/map_selector_button", self.singlePlayer.loadLevel, Vec3(0.94,0,-0.67), 
			isMovie=False, scale=Vec3(0.5, 2, 0.15), text="Ok", textPos=Vec3(-0.05,0,-0.0125), textScale=Vec3(0.075,0.075,0.05))
			
		self.buttons['cancel'] = MenuButton(self.parent, "cancel", "models/menu/hud/map_selector_button", self.exit, Vec3(0.975,0,-0.845), 
			isMovie=False, scale=Vec3(0.5, 2, 0.15), text="Cancel", textPos=Vec3(-0.125,0,-0.0125), textScale=Vec3(0.075,0.075,0.05))
	
		self.mapNameNode = TextNode('map_name_text')
		self.mapNameNode.setText("No Level Selected")
		self.mapNameModel = self.parent.base.aspect2d.attachNewNode(self.mapNameNode)
		self.mapNameModel.setPos(0.55,0,0.65)
		self.mapNameModel.setColor(0.3, 1, 0.3, 1)
		self.mapNameModel.setScale(0.05)
		
		self.selected = ""
	
	def setCustomSelection(self):
		self.setSelection(self.input.get(plain=True))
	
	def setSelection(self, key):
		if (os.path.exists("maps/" + key + ".lvl")):
			self.selected = key
			file = open("maps/" + key + ".lvl", "r")
			name = file.readline()
			self.mapNameNode.setText(name)
			file.close()
		
	def focusInput(self):
		self.input.enterText('')
		
	def exit(self):
		self.destroy()
		self.parent.mainMenu.show()
		
	def destroy(self):
		for key in self.buttons.keys():
			self.buttons[key].removeNode()
		
		self.input.removeNode()
		self.background.removeNode()
		self.mapNameModel.removeNode()
		if (self.dialog):
			self.dialog.removeNode()
		
	def showInformationDialog(self):
		if (not self.dialog):
			self.dialog = Dialog(self.parent, 'information_dialog')
			self.dialog.addTextBox('In order to play: ')
			self.dialog.addTextBox('Click a level in the levels box and then click ok to load that level', pos=Vec3(0, 0, -0.05))	
			self.dialog.addOkCancel(self.dialog.hide, only='ok')
		else:
			self.dialog.show()
		